/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package image;

import hash.FeatureVectorIdentity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import configuration.GenePoolConfiguration;
import configuration.VisualRecognitionConfiguration;

/**
 * @author Ashish
 *
 */
public class ImageSet {
	private ArrayList<Image> images = new ArrayList<Image>();
	private ImageList imageList = null;
	private ArrayList<FeatureVectorIdentity> featureVectorIdentities = new ArrayList<FeatureVectorIdentity>();
	
	public ImageSet(VisualRecognitionConfiguration config, GenePoolConfiguration geneConfig) throws IOException{
//		setImageList(config.imageSetURI);
		this.imageList = new ImageList(config.imageSetURI);
		setImages(config, geneConfig);
	calcFeatureVectorIdentities();
	}
	/**
	 * @param geneConfig 
	 * @param images the images to set
	 * @throws IOException 
	 */
	public void setImages(VisualRecognitionConfiguration config, GenePoolConfiguration geneConfig) throws IOException {
		int numImages = this.imageList.getImageNames().size();
		for(int i=0;i<numImages;i++){
			Image image = new Image(config, geneConfig);
			image.setImageName(this.imageList.getImageNames().get(i));
			image.setClassLabel(this.imageList.getClassLabels().get(i));
			
			String descriptorURI = config.descriptorURL + image.getImageName() + ".key";
			File descriptorFile = new File(descriptorURI);
			image.setNumKeypoints(descriptorFile);
//			image.setFeatureVectors(descriptorFile);
			this.images.add(image);	
		}
	}

	/**
	 * @return the images
	 */

	public ArrayList<Image> getImages() {
		return images;
	}

	/**
	 * @param imageList the imageList to set
	 * @throws IOException 
	 */
	public void setImageList(String imageSetURI) throws IOException {
		this.imageList = new ImageList(imageSetURI);
	}

	/**
	 * @return the imageList
	 */
	public ImageList getImageList() {
		return this.imageList;
	}

	/* (non-Javadoc)
	 * @see feature.FeatureSpace#writeFeatureSpaceToFile()
	 */

	/*
	public File writeFeatureSpaceToFile(VisualRecognitionConfiguration config) throws IOException {
		String fileName = config.dataURL + "featureSpace" + ".csv";
		File file = new File(fileName);
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator<Image> itr_image = images.iterator();
		while(itr_image.hasNext()){
			int numFeatureVectors = itr_image.next().getFeatureVectors().size();
			for(int i=0;i<numFeatureVectors;i++){
				int numDimensions = itr_image.next().getFeatureVectors().get(i).getFeatureVector().size();
				for(int j=0;j<numDimensions-1;j++){
					bw.write(itr_image.next().getFeatureVectors().get(i).getFeatureVector().get(j));
					bw.write(",");
				}
				bw.write(itr_image.next().getFeatureVectors().get(i).getFeatureVector().get(numDimensions-1));
				bw.write("\n");
			}
		}
		bw.close();
		fw.close();
		return file;
	}
*/
	
	/* (non-Javadoc)
	 * @see feature.FeatureSpace#calcFeatureSpace()
	 */
	/*
	@Override
	public ArrayList<ArrayList<Integer>> calcFeatureSpace() {
		ArrayList<ArrayList<Integer>> featureSpace = new ArrayList<ArrayList<Integer>>();
		Iterator<Image> itr_image = images.iterator();
		while(itr_image.hasNext()){
			Iterator<FeatureVector> itr_fv = itr_image.next().getFeatureVectors().iterator();
			featureSpace.add(itr_fv.next().getFeatureVector());
		}
		return featureSpace;
	}
*/
	/* (non-Javadoc)
	 * @see feature.FeatureSpace#writeFeatureVectorIdentityToFile(configuration.VisualRecognitionConfiguration)
	 */
	/*
	@Override
	public File writeFeatureVectorIdentityToFile(
			VisualRecognitionConfiguration config) throws IOException {
		String fileName = config.dataURL + "featureVectorIdentity" + ".csv";
		File file = new File(fileName);
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		Iterator<FeatureVectorIdentity> itr = featureVectorIdentities.iterator();
		while(itr.hasNext()){
			bw.write(itr.next().toString());
			bw.write("\n");
		}
		bw.close();
		fw.close();
		return file;
	}
*/
	
	public void calcFeatureVectorIdentities(){		
		for(int j = 0; j < imageList.getImageNames().size();j++){			
			for(int i =0; i < this.images.get(j).getNumKeypoints();i++){
				FeatureVectorIdentity fvi = new FeatureVectorIdentity();
				fvi.setImageName(this.imageList.getImageNames().get(j));
				fvi.setIndexInImage(i+1);
				fvi.setImageIdx(j+1);
				fvi.setClassLabel(this.imageList.getClassLabels().get(j).intValue());
				this.featureVectorIdentities.add(fvi);
			}
		}
	}
	
	
	/**
	 * @return the featureVectorIdentity
	 */
	
	public ArrayList<FeatureVectorIdentity> getFeatureVectorIdentities() {
		return featureVectorIdentities;
	}

	
	
}
